<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="showInsert"
        >{{ $t('insert') }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tagList"
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        :label="$t('name')"
      />
      <el-table-column
        :label="$t('time')"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 13px">{{
            formatDate(scope.row.createdDate)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('function')" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          >{{ $t('edit') }}
          </el-button>
          <el-button
            style="color: #f56c6c"
            type="text"
            icon="el-icon-delete"
            @click="del(scope.row)"
          >{{ $t('delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogTagVisible"
      top="15px"
    >
      <el-form
        label-width="20%"
        :model="tag"
      >
        <el-form-item :label="$t('name')">
          <el-input v-model="tag.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTagVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="addOrUpdate">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { cancelNotice, formatDate, successNotice } from '@/util/common'
import { deleteTag, insertTag, listTag, updateTag } from '@/api/iot/iotTagApi'

export default {
  name: 'TagManage',
  data() {
    return {
      tagList: [],
      tag: {},
      dialogTagVisible: false
    }
  }, created() {
    this.listTag()
  },
  methods: {
    formatDate(time) {
      return formatDate(this, time)
    },
    showInsert() {
      this.tag = {}
      this.dialogTagVisible = true
    },
    listTag() {
      listTag({}).then(res => {
        const { code, data } = res
        if (code === '200') {
          this.tagList = data
        } else {
          this.tagList = []
        }
      })
    },
    addOrUpdate() {
      const json = {
        ...this.tag
      }
      if (!json.tagId) {
        insertTag(json).then(res => {
          const { code } = res
          if (code === '200') {
            this.dialogTagVisible = false
          }
          this.listTag()
        })
      } else {
        updateTag(json).then(res => {
          const { code } = res
          if (code === '200') {
            this.dialogTagVisible = false
          }
          this.listTag()
        })
      }
    },
    edit(item) {
      this.tag = item
      this.dialogTagVisible = true
    },

    del(item) {
      const that = this
      this.$confirm(this.$t('delete') + item.name, this.$t('prompt'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      })
        .then(function() {
          deleteTag({
            tagId: item.tagId
          }).then(res => {
            const { code } = res
            if (code === '200') {
              successNotice(that)
              that.listTag()
            }
          })
        })
        .catch(function() {
          cancelNotice(that)
        })
    }
  }
}
</script>

